package com.ruoyi.business.service;

import com.ruoyi.business.domain.statistics.StatisticsBO;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 涉及到统计服务通用处理接口
 *
 * @author YW
 * @create 2021/7/1
 */
public abstract class StatisticsService<T extends StatisticsBO> {

    @Autowired
    private ISysDictDataService dictDataService;

    public List<T> getStatisticsByDictType(String dictType, List<T> rsList) {
        List<SysDictData> dictDatas = dictDataService.selectDictDataByType(dictType);
        return dictDatas.stream().map(e -> getStatisticsIntBO(e, rsList)).collect(Collectors.toList());
    }

    private T getStatisticsIntBO(SysDictData dict, List<T> rsList) {
        Optional<T> statisticsIntOpt = rsList.stream().filter(e -> dict.getDictValue().equals(e.getCode())).findFirst();
        if (statisticsIntOpt.isPresent()) {
            T statisticsIntBO = statisticsIntOpt.get();
            statisticsIntBO.setValue(dict.getDictLabel());
            return statisticsIntBO;
        }
        return initDefaultStatistics(dict);
    }

    /**
     * 初始化一个默认统计记录
     *
     * @param dictDatas 枚举数据
     * @return
     */
    protected abstract T initDefaultStatistics(SysDictData dictDatas);

}
